/**
 * employee管理初始化
 */
var Employee = {
    id: "EmployeeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    width: '950px',
    height: '580px'
};

var params = {

};

/**
 * 初始化表格的列
 */
Employee.initColumn = function () {
    return [
        {field: 'selectItem', radio: false},
            {title: '员工姓名', field: 'empName', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '员工编码', field: 'empCode', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '二级系统ERP编号', field: 'erpCode', visible: false, align: 'center', valign: 'middle', sortable: true},
            {title: '部门(单位）', field: 'deptName', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '处/室', field: 'placeName', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '班组', field: 'teamName', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '岗位类别', field: 'postType', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '职务（岗位）', field: 'post', visible: true, align: 'center', valign: 'middle', sortable: true},
            {title: '用工类型', field: 'laborType', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '创建日期', field: 'createDate', visible: false, align: 'center', valign: 'middle',sortable: true},
        {title: '数据来源', field: 'createType', visible: false, align: 'center', valign: 'middle',sortable: true}
            ];
};

/**
 * 检查是否选中
 */
Employee.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Employee.seItem = selected[0];
        return true;
    }
};

Employee.checks = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Employee.seItem = selected;
        return true;
    }
};


/**
 * 点击添加employee
 */
Employee.openAddEmployee = function () {
    var index = layer.open({
        type: 2,
        title: '添加员工',
        area: [Employee.width, Employee.height], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/employee/employee_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看employee详情
 */
Employee.openEmployeeDetail = function () {
    if (this.check()) {
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        if(selected.length > 1){
            Feng.info("修改只能选择一条数据！");
            return false;
        }
        var index = layer.open({
            type: 2,
            title: '员工详情',
            area: [Employee.width, Employee.height], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/employee/employee_update/' + Employee.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除employee
 */
Employee.delete = function () {
    if (this.checks()) {
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/employee/delete", function (data) {
                Feng.success("删除成功!");
                Employee.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = "";
            for (var i in Employee.seItem) {
                if (!ids) {
                    ids += Employee.seItem[i].id;
                } else {
                    ids += "," + Employee.seItem[i].id;
                }
            }
            ajax.set("employeeId", ids);
            ajax.start();
        };
        Feng.confirm("是否删除选中的"+Employee.seItem.length +"数据？", operation);
    }
};

/**
 * 查询employee列表
 */
Employee.search = function () {
    var queryData = {};
    Employee.table.setQueryParams(queryData);

    queryData['condition'] = $("#condition").val();
    Employee.table.refresh({query: queryData});
};

/**
 * 高级搜索
 */
Employee.g_search = function () {
    var index = layer.open({
        type: 2,
        skin: 'layui-layer-molv',
        title: '高级搜索',
        area: ['800px', '320px'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/employee/g_search',
        btn: ['确定','关闭'],
        yes: function(index, layero){
            var queryData = window["layui-layer-iframe" + index].Employee.search2();

            Employee.table.setQueryParams(queryData);
            Employee.table.refresh();
            Employee.table.empty();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
    });
    this.layerIndex = index;
};

Employee.search2 = function () {
    var queryData = {};
    var department = $("#department option:selected").val();
    var place = $("#place option:selected").val();
    var team = $("#team option:selected").val();
    var postType =$("#postType option:selected").val();

    var min = $("#min").val();
    var max = $("#max").val();
    if(department){
        queryData.department = department;
    }
    if(place){
        queryData.place = place;
    }
    if(team){
        queryData.team = team;
    }
    if(postType){
        queryData.postType = postType;
    }
    if(min){
        queryData.min = min;
    }
    if(max){
        queryData.max = max;
    }
    return queryData;
};

/**
 * 导入employee数据
 */
Employee.import = function () {
    var index = layer.open({
        type: 2,
        title: '导入员工',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/employee/employee_import'
    });
    this.layerIndex = index;
};


Employee.callbackdata = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    return selected[0];
};

Employee.export = function(){
    window.open(Feng.ctxPath + '/employee/export',"_parent");
    Feng.success("导出成功!");
};

$(function () {
    var defaultColunms = Employee.initColumn();
    var table = new BSTable(Employee.id, "/employee/list", defaultColunms);
    table.setPaginationType("client");
    Employee.table = table.init();
});
